﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SharpDiary.Core.Utility;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Controls;
using SharpDiary.Core.Attributes;
using SharpDiary.Core.Security;

namespace SharpDiary.Diary
{
    [SettingsAttribute("80592bec-d40c-435c-be3a-0732834879ae", "DiarySettingsControl")]
    public partial class DiarySettingsControl : SettingsControlBase
    {
        private UserInfo _defaultUser;

        private bool _isPasswordModified = false; //密码是否被用户修改

        public DiarySettingsControl()
        {
            InitializeComponent();
        }

        public override bool CheckIfInputOk(ref string errorMessage)
        {
            errorMessage = string.Empty;

            if (txtUserName.Text.Trim().Length == 0)
            {
                errorMessage = "请填写用户名称。";
                txtUserName.Focus();

                return false;
            }

            if (txtKey.Text.Trim().Length == 0)
            {
                errorMessage = "请填写密钥。";
                return false;
            }

            return true;
        }

        public override void LoadSettingsInfo()
        {
            _defaultUser = UserInfo.GetDefaultUserInfo();
            if (_defaultUser != null)
            {
                txtUserName.Text = _defaultUser.SignInUserName;
                txtPassword.Text = UserSettings.ShellInstance.CryptionService.Decrypt(_defaultUser.Password);
                txtKey.Text = UserSettings.ShellInstance.CryptionService.Decrypt(_defaultUser.PrivateKey);

                txtKey.Enabled = DiaryInfo.GetTotalDiaryCount() == 0;
            }

            _isPasswordModified = false;
        }

        public override bool SaveSettingsInfo()
        {
            if (_defaultUser == null)
            {
                UserInfo newUser = new UserInfo();
                newUser.SignInUserName = txtUserName.Text;
                if (_isPasswordModified)
                {
                    newUser.Password = MD5Helper.GenerateToMD5HexString(txtPassword.Text);
                }
                newUser.PrivateKey = UserSettings.ShellInstance.CryptionService.Encrypt(txtKey.Text);
                newUser.UserId = Guid.NewGuid().ToString();
                newUser.CreatedDatetime = DateTime.Now;
                newUser.UpdateDatetime = DateTime.Now;

                return UserInfo.Create<UserInfo>(newUser);
            }
            else
            {
                _defaultUser.SignInUserName = txtUserName.Text;
                if (_isPasswordModified)
                {
                    _defaultUser.Password = MD5Helper.GenerateToMD5HexString(txtPassword.Text);
                }

                if (txtKey.Enabled)
                {
                    _defaultUser.PrivateKey = UserSettings.ShellInstance.CryptionService.Encrypt(txtKey.Text);
                }

                return UserInfo.Update<UserInfo>(_defaultUser);
            }
        }

        private void txtPassword_ModifiedChanged(object sender, EventArgs e)
        {
            _isPasswordModified = true;
        }

    }
}
